
import SSF.OS.*;
import SSF.Net.*;
import com.renesys.raceway.DML.*;

public class pingClient extends ProtocolSession {
    
    int destip,ct,bytes;

    double ival;
    
    long pingStart;

    public void config(Configuration cfg) throws configException {
	super.config(cfg);

	String dest = (String)cfg.findSingle("dest");
	String count = (String)cfg.findSingle("count");
	String interval = (String)cfg.findSingle("interval");
	String len = (String)cfg.findSingle("len");

	Host host = (Host)inGraph();
	destip = SSF.Net.Util.IP_s.StrToInt(host.getNet().nhi_to_ip(dest));
	ct = (null==count?1:Integer.parseInt(count));
	ival = (null==interval?1.:Double.parseDouble(interval));
	bytes = (null==len?48:Integer.parseInt(len));
    }

    public void init() {
	// after 'ival' seconds, do the configured pinging.
	(new Timer(inGraph(),Net.seconds(ival)){
		public void callback() {
		    try {
			ICMP icmp = (ICMP)inGraph().SessionForName("icmp");
			icmp.listen(pingClient.this);
			pingStart = inGraph().now();
			icmp.ping(bytes,destip,ct,ival);
		    } catch (ProtocolException pex) {
			pex.printStackTrace();
		    }
		}
	    }).set();
    }

    public boolean push(ProtocolMessage msg, ProtocolSession from) {
	if (msg instanceof ICMPHeader) {
	    ICMPHeader ih = (ICMPHeader)msg;
	    System.out.println("** PingClient received "+ih+" after "+
			       (inGraph().now()-pingStart)*
			       1./Net.frequency+" seconds");
	}
	return true;
    }
}
